"""
员工模型
"""
from datetime import datetime
from app.extensions import db


class Staff(db.Model):
    """项目负责人员表"""
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), nullable=False, comment='姓名')
    project_name = db.Column(db.String(100), comment='主要所属项目')
    position = db.Column(db.String(50), comment='主要职务')
    specific_position = db.Column(db.String(100), comment='具体职务')
    is_general_manager = db.Column(db.Boolean, default=False, comment='是否总经理')
    phone = db.Column(db.String(20), comment='电话')
    customers = db.Column(db.Text, comment='对应客户（JSON格式）')
    project_level = db.Column(db.String(20), comment='项目级别：省一级、市一级、区县一级')
    project_create_time = db.Column(db.DateTime, comment='项目搭建时间')
    project_optimize_time = db.Column(db.DateTime, comment='项目优化时间')
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    
    # 为兼容性添加的字段
    email = db.Column(db.String(100), comment='邮箱')
    contact_info = db.Column(db.String(200), comment='联系信息')
    
    def __repr__(self):
        return f'<Staff {self.name}>'


class StaffProject(db.Model):
    """员工项目关联表 - 支持一个员工对应多个项目"""
    id = db.Column(db.Integer, primary_key=True)
    staff_id = db.Column(db.Integer, db.ForeignKey('staff.id'), nullable=False)
    project_name = db.Column(db.String(100), nullable=False, comment='项目名称')
    project_level = db.Column(db.String(20), comment='项目级别：省一级、市一级、区县一级')
    position_in_project = db.Column(db.String(50), comment='在该项目中的职务')
    specific_position_in_project = db.Column(db.String(100), comment='在该项目中的具体职务')
    is_active = db.Column(db.Boolean, default=True, comment='是否有效')
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    
    # 为兼容性添加的字段
    role = db.Column(db.String(50), comment='角色')
    
    # 关联
    staff = db.relationship('Staff', backref=db.backref('staff_projects', lazy=True))
    
    def __repr__(self):
        return f'<StaffProject {self.staff_id}-{self.project_name}>'



